<?php
/**
 * @package qBase
 * @subpackage web
 */

/**
 * QWebResponse represents a web response
 * 
 * @package qBase
 * @subpackage web
 *
 */
final class QWebResponse extends QObject 
{
	/**
	 * The output string to be send to the browser
	 *
	 * @var string
	 */
	protected $out;
	
	/**
	 * The headers to be sent to the browser
	 *
	 * @var array
	 */
	protected $headers;
	
	/**
	 * The constructor
	 *
	 * @param boolean $default
	 */
	public function __construct($default = false)
	{
		if (!$default)
			throw new Exception("Only the default web response is implemented");
	}

	/**
	 * Sets a header for this respponse. If the header is not key/value leave value empty
	 *
	 * @param string $key
	 * @param string $value
	 */
	public function setHeader($key, $value = null)
	{
		if (!$this->headers)
			$this->headers = array();
		$this->headers[$key] = $value;
	}
	
	/**
	 * Gets a header that has been set for this response
	 *
	 * @param string $key
	 * @return string
	 */
	public function getHeader($key)
	{
		if ($this->headers && isset($this->headers[$key]))
			return $this->headers[$key];
		else 
			return null;
	}

	/**
	 * Sends the headers and the output to the browser
	 *
	 */
	public function send()
	{
		if ($this->headers)
			foreach ($this->headers as $key => $value)
			{
				if (strlen($value) > 0)
					header("$key: $value");
				else 
					header($key);
			}
		echo $this->out;
	}

	/**
	 * Appens a string or number to the output string
	 *
	 * @param string $string
	 */
	public function append($string)
	{
		$this->out .= $string;
	}
	
	/**
	 * Gets the current output string
	 *
	 * @return string
	 */
	public function getCurrentOutput()
	{
		return $this->out;
	}
}


?>